{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f71d0d52-686f-485d-8988-d197c972e34b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from efficient_apriori import apriori"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d09192ac-9fb9-4864-a3b8-6c4299360e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造数据集\n",
    "data = [('牛奶','面包','尿不湿','啤酒','榴莲'),\n",
    "        ('可乐','面包','尿不湿','啤酒','牛仔裤'),\n",
    "        ('牛奶','尿不湿','啤酒','鸡蛋','咖啡'),\n",
    "        ('面包','牛奶','尿不湿','啤酒','睡衣'),\n",
    "        ('面包','牛奶','尿不湿','可乐','鸡翅')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5ff99c57-484f-46a5-bb69-7deb3b0e0694",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 挖掘频繁项集和频繁规则\n",
    "itemsets, rules = apriori(data, min_support=0.6,  min_confidence=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4d82e208-3598-4e2e-aef7-584398bc21d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: {('牛奶',): 4, ('面包',): 4, ('尿不湿',): 5, ('啤酒',): 4},\n",
       " 2: {('啤酒', '尿不湿'): 4,\n",
       "  ('啤酒', '牛奶'): 3,\n",
       "  ('啤酒', '面包'): 3,\n",
       "  ('尿不湿', '牛奶'): 4,\n",
       "  ('尿不湿', '面包'): 4,\n",
       "  ('牛奶', '面包'): 3},\n",
       " 3: {('啤酒', '尿不湿', '牛奶'): 3, ('啤酒', '尿不湿', '面包'): 3, ('尿不湿', '牛奶', '面包'): 3}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输出频繁项集\n",
    "itemsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7386c456-7368-4bd4-955f-505e832beeb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('牛奶',): 4, ('面包',): 4, ('尿不湿',): 5, ('啤酒',): 4}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "itemsets[1] # 满足条件的一元组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3a4757a5-69cc-4197-aeee-b7e53102a248",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('啤酒', '尿不湿'): 4,\n",
       " ('啤酒', '牛奶'): 3,\n",
       " ('啤酒', '面包'): 3,\n",
       " ('尿不湿', '牛奶'): 4,\n",
       " ('尿不湿', '面包'): 4,\n",
       " ('牛奶', '面包'): 3}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "itemsets[2] # 满足条件的二元组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "76009b35-1a1a-487f-bf27-5397bd51a550",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('啤酒', '尿不湿', '牛奶'): 3, ('啤酒', '尿不湿', '面包'): 3, ('尿不湿', '牛奶', '面包'): 3}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "itemsets[3] # 满足条件的三元组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "40eb43ae-d2a5-4d15-8dd1-20994256bb03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{啤酒} -> {尿不湿},\n",
       " {牛奶} -> {尿不湿},\n",
       " {面包} -> {尿不湿},\n",
       " {啤酒, 牛奶} -> {尿不湿},\n",
       " {啤酒, 面包} -> {尿不湿},\n",
       " {牛奶, 面包} -> {尿不湿}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频繁规则\n",
    "rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3397de87-a74d-48cc-b6e1-cbdd907f577c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把max_length=2这个参数加进去\n",
    "itemsets, rules = apriori(data, min_support=0.6,\n",
    "                            min_confidence=0.5,\n",
    "                            max_length=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "679c62e0-da84-4022-a310-9250a7f1da59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: {('牛奶',): 4, ('面包',): 4, ('尿不湿',): 5, ('啤酒',): 4},\n",
       " 2: {('啤酒', '尿不湿'): 4,\n",
       "  ('啤酒', '牛奶'): 3,\n",
       "  ('啤酒', '面包'): 3,\n",
       "  ('尿不湿', '牛奶'): 4,\n",
       "  ('尿不湿', '面包'): 4,\n",
       "  ('牛奶', '面包'): 3}}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频繁项集\n",
    "itemsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "30420869-912a-4209-9762-5df701e18d28",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把宁浩导演过的电影主演数据转换成列表\n",
    "data = [['葛优','黄渤','范伟','邓超','沈腾','张占义','王宝强','徐峥','闫妮','马丽'],\n",
    "['黄渤','张译','韩昊霖','杜江','葛优','刘昊然','宋佳','王千源','任素汐','吴京'], \n",
    "['郭涛','刘桦','连晋','黄渤','徐峥','优恵','罗兰','王迅'],\n",
    "['黄渤','舒淇','王宝强','张艺兴','于和伟','王迅','李勤勤','李又麟','宁浩','管虎','梁静','徐峥','陈德森','张磊'],\n",
    "['黄渤','沈腾','汤姆·派福瑞','马修·莫里森','徐峥','于和伟','雷佳音','刘桦','邓飞','蔡明凯','王戈','凯特·纳尔逊','王砚伟','呲路'],\n",
    " ['徐峥','黄渤','余男','多布杰','王双宝','巴多','杨新鸣','郭虹','陶虹','黄精一','赵虎','王辉'],\n",
    " ['黄渤','戎祥','九孔','徐峥','王双宝','巴多','董立范','高捷','马少骅','王迅','刘刚','WorapojThuantanon','赵奔','李麒麟','姜志刚','王鹭','宁浩'],\n",
    " ['黄渤','徐峥','袁泉','周冬雨','陶慧','岳小军','沈腾','张俪','马苏','刘美含','王砚辉','焦俊艳','郭涛'],\n",
    " ['雷佳音','陶虹','程媛媛','山崎敬一','郭涛','范伟','孙淳','刘桦','黄渤','岳小军','傅亨','王文','杨新鸣']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ef645360-dc70-419b-9f07-8641f48ae6c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 算法应用\n",
    "itemsets, rules = apriori(data, min_support=0.5,  min_confidence=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4aa0bca0-9504-4f44-a4da-fd37d1ebe04d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: {('黄渤',): 9, ('徐峥',): 7}, 2: {('徐峥', '黄渤'): 7}}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "itemsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f26936b4-e34d-4fb6-b795-d4298222ce41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{徐峥} -> {黄渤}]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afee4ccc-62b5-408d-97c6-9f24e4ba65dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
